<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-10-05 14:55:13
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\LoomRepository;
use App\Repositories\OpenCardDetailsRepository;
use App\Repositories\OpenCardDetailsSchedulingRepository;
use DB;

/**
 * @property LoomRepository $repository 
 */
class SchedulingService extends Service
    {
    public function boot(LoomRepository $repository)
        {
        $this->repository = $repository;
        }
    public function conditionDate()
        {
        $date    = null;
        $request = request();
        if ($request->has("filter")) {
            $filters = $request->filter;
            foreach ($filters ?? [] as $keys => $filter) {
                foreach ($filter ?? [] as $key => $value) {
                    if ($key == 'date') {
                        $date = $filters[$keys]['date'];
                        unset($filters[$keys]['date']);
                        }
                    }
                }
            $request->offsetSet("filter", $filters);
            }
        return $date ?? date("Y-m-d");
        }
    public function get_need_scheduling()
        {
        return OpenCardDetailsSchedulingRepository::where(function ($query)
            {
            $query->orWhere("machine_id", "=", 0)->orWhereNull("machine_id");
            })->whereHas("open_card_details", function ($q)
                {
                $q->where("is_sample", "<>", "1");
                $q->whereHas("wip", function ($query)
                    {
                    $query->where("faultle_quantity", ">", "0");
                    });
                })->condition()->with(['material', 'customer', 'point_stick', 'glue', 'open_card_details', 'process_route'])->getGrid();
        }
    public function list()
        {
        $date = $this->conditionDate();
        $data = $this->repository->setRelation([
            'open_card_details_scheduling' => function ($query) use ($date)
                {
                $date .= " 08:00:00";
                $query->where(function ($q) use ($date)
                    {
                    $q->orWhereBetween("created_at", [$date, date("Y-m-d h:i:s", strtotime($date) + 86400 * 3)]);
                    $q->orWhereBetween("audited_at", [$date, date("Y-m-d h:i:s", strtotime($date) + 86400 * 3)]);
                    $q->orWhereNull("audited_at");
                    });
                $query->with(['material', 'customer', 'point_stick', 'glue', 'open_card_details', 'process_route']);
                $query->orderBy("sort", "asc");
                }
        ])->whereCategory_id(3)
            ->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        $data['date'] = $date;
        $data['rows'] = $data['rows']->map(function ($v)
            {
            $v['quantity']        = $v->open_card_details_scheduling()->where("created_at", ">=", date("Y-m-d h:i:s"))->sum('quantity');
            $v['finish_quantity'] = $v->open_card_details_scheduling()->where("audited_at", ">=", date("Y-m-d h:i:s"))->sum('quantity');
            return $v;
            });
        return $data;
        }
    }